package org.proddevplm.dao.core;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IProductMgmt;
import org.proddevplm.dao.data.Product;
import org.proddevplm.dao.data.Project;
import org.proddevplm.dao.util.HibernateUtil;

public class ProductMgmt implements IProductMgmt{

	@Override
	public boolean addProduct(Product product) {
		
		HibernateUtil.currentSession().persist(product);
		
		return false;
	}

	@Override
	public List<Product> getProductsByProjectId(int projectId) {
		
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select p from Product p where p.project_id=:projectId");
		q.setInteger("projectId", projectId);
		
		return q.list();
	}

	@Override
	public Product getProductById(int productId) {
		
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select p from Product p where p.id=:productId");
		q.setInteger("productId", productId);
		
		Product product = (Product)q.uniqueResult();
				
		return product;
	}

	@Override
	public boolean mergeProductInfo(Product product) {
		
		HibernateUtil.currentSession().merge(product);
		
		return false;
	}

	@Override
	public List<Product> getAllProducts() {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select p from Product as p");
		return q.list();
	}

}
